
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { overdueDistributionS1Action } from './index';
import moment from 'moment';
import SearchList from './SeachList/SearchList';
import CardDemo from './cardDemo';
import { axios } from 'utils';

class overdueDistributionS1 extends Component{
    constructor(props) { 
        super(props);
        this.state = {
            initTime: moment(),
            tableData: {
                zu: [],
                member: []
            }
        };
    }

    convertParams = (obj) => {
        return {
            dayTime: obj.time ? obj.time.format('YYYY-MM-DD') : '',
            departmentId: obj.departmentId || '',
            departmentName: obj.departmentName || '',
            day: obj.day || '3'
        };
    }

    getTableData = (data) => {
        axios({
            url: '/hs/admin/statistics/overdueDistributionS1',
            method: 'post',
            data
        }).then((res) => {
            if(res.code === 200) {
                this.setState({
                    tableData: res.data
                })
            }

        })
    }

    handleSearch = (obj) => {
        // const { getTableData } = this.props;
        const { personData } = this.props;
        setTimeout(()=> {
            personData.map((item)=>{
                if(Number(item.value) === Number(obj.departmentId)){
                    obj.departmentName = item.name
                }
            })
            const params = this.convertParams(obj);
            this.getTableData({ ...params });
        },0)
    }

    componentDidMount() {
        const { getPerson } = this.props;
        getPerson();
    }
    
    render() {
        const { initTime, tableData } = this.state;
        const { loading, personData } = this.props;
        const { zu, member } = tableData
        console.log(tableData)
        console.log(member)
        return (
            <div>
                <SearchList handleSearch={this.handleSearch} initTime={initTime} loading={loading} personData={personData}/>
                <CardDemo data={zu} title={`整组数据`}/>
                {
                    member ? member.map((item)=> {
                        return (
                            <div style={{marginTop: '20px'}} key={item.name}><CardDemo data={item} title={`个人信息-${item.name}`}/></div>
                        )
                    }) : null
                }
            </div>
        );

    }
}

const mapStateToProps = (state) => {
    const { businessStatisticsState: { overdueDistributionS1State } } = state;
    return {
        tableData: overdueDistributionS1State['tableData'],
        loading: overdueDistributionS1State['loading'],
        searchParams: overdueDistributionS1State['params'],
        personData: overdueDistributionS1State['personData'],
        selectKeys: overdueDistributionS1State['selectKeys'],
        personType: overdueDistributionS1State['personType']
    };
}
const mapDispatchToProps = (dispatch) => {
    return bindActionCreators({
        getTableData: overdueDistributionS1Action.ASDEGetTableData,
        setTableData: overdueDistributionS1Action.ASDESetTableData,
        getPerson: overdueDistributionS1Action.ASDEGetPerson,
    }, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(overdueDistributionS1);